package com.rongcheng.frc.newstyledemo.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongcheng.frc.androidlib.activity.BaseActivity;
import com.rongcheng.frc.androidlib.net.RequestCallback;
import com.rongcheng.frc.newstyledemo.activity.login.LoginActivity;
import com.rongcheng.frc.newstyledemo.engine.AppConstants;

public abstract class AppBaseActivity extends BaseActivity {
    protected boolean needCallback;

    protected ProgressDialog dlg;
    protected ProgressDialog YXDlg;


    public ImageLoader imageLoader = ImageLoader.getInstance();

    protected void onDestroy() {
        //回收该页面缓存在内存的图片
        imageLoader.clearMemoryCache();

        super.onDestroy();
    }


    public abstract class AbstractRequestCallback implements RequestCallback {

        public abstract void onSuccess(String content, boolean isMock);

        public void onFail(String errorMessage, boolean isMock) {
//            if (dlg != null)
//            dlg.dismiss();
            if (YXDlg != null) {
                YXDlg.dismiss();
            }
            Log.e("frc", "Mock is :" + isMock);
            new AlertDialog.Builder(AppBaseActivity.this).setTitle("出错啦")
                    .setMessage(errorMessage).setPositiveButton("确定", null)
                    .show();
        }

        public void onCookieExpired() {
//            if (dlg != null)
//                dlg.dismiss();
            if (YXDlg != null) {
                YXDlg.dismiss();
            }
            new AlertDialog.Builder(AppBaseActivity.this)
                    .setTitle("出错啦")
                    .setMessage("Cookie过期，请重新登录")
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    Intent intent = new Intent(
                                            AppBaseActivity.this,
                                            LoginActivity.class);
                                    intent.putExtra(AppConstants.NeedCallback,
                                            true);
                                    startActivity(intent);
                                }
                            }).show();
        }
    }

    public void showDlg() {
        YXDlg.show();
    }
}
